package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.EventInfoMapper;
import cc.rengu.igas.bsps.common.entity.EventInfo;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 事件申报信息表操作接口实现类
 * Created by 王成 on 2018/10/8.
 */
public class EventInfoMapperImpl implements EventInfoMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.BSPS_POOL_NAME);

    @Override
    public int insertEventInfo(EventInfo eventInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.BSPS_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertEventInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, eventInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记事件申报信息失败，iReturnCode:<{}>", iReturnCode);
            return iReturnCode;
        }
        return iReturnCode;
    }

    @Override
    public EventInfo selectEventInfoByPrimaryKey(String instId, String eventId) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.BSPS_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectEventInfoByPrimaryKey");
        EventInfo eventInfo = new EventInfo();
        eventInfo.setInstId(instId);
        eventInfo.setEventId(eventId);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, eventInfo, eventInfo);
        if (0 != iReturnCode) {
            rglog.error("根据机构标识<{}>及事件标识<{}>查询事件申报信息失败！", instId, eventId);
            return null;
        }
        return eventInfo;
    }

    @Override
    public int updateEventInfoByPrimaryKey(EventInfo eventInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.BSPS_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateEventInfoByPrimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, eventInfo, null);
        if (0 != iReturnCode) {
            rglog.error("根据机构标识<{}>及事件标识<{}>更新事件申报信息失败，iReturnCode:<{}>", eventInfo.getInstId(), eventInfo.getEventId(), iReturnCode);
            return iReturnCode;
        }
        return iReturnCode;
    }

    @Override
    public EventInfo selectEventInfoByDeclareSeqNum(String instId, String declareSeqNum) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.BSPS_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectEventInfoByDeclareSeqNum");
        EventInfo eventInfo = new EventInfo();
        eventInfo.setInstId(instId);
        eventInfo.setDeclareSeqNum(declareSeqNum);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, eventInfo, eventInfo);
        if (0 != iReturnCode) {
            rglog.error("根据机构标识<{}>及申报流水号<{}>查询事件申报信息失败！", instId, declareSeqNum);
            return null;
        }
        return eventInfo;
    }

    @Override
    public List<EventInfo> selectEventInfoByDeclareDate(String instId, String startDate, String endDate) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.BSPS_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectEventInfoByDeclareDate");
        EventInfo eventInfo = new EventInfo();
        eventInfo.setInstId(instId);
        eventInfo.setDeclareDate(startDate);
        eventInfo.setRemark1(endDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, eventInfo, EventInfo.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据机构标识<{}>获取全量事件申报信息失败!", instId);
            return null;
        }
        return objectList.stream().map(item -> (EventInfo) (item)).collect(Collectors.toList());
    }

    @Override
    public PageResult selectEventInfoByDeclareDatePage(EventInfo eventInfo, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.BSPS_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectEventInfoByDeclareDatePage");

        return dbsUtil.dbsFetchPageListData(sqlStr, eventInfo, EventInfo.class, pageInfo);
    }

}
